package com.osmino.contacts.entity;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import com.devoteam.quickaction.QuickItem;
import com.osmino.contacts.R;
import com.osmino.contacts.provider.SendMessageUtils;

public final class QuickItemInflater {

	private static final int INFO_DRAWABLE = R.drawable.qcb_ic_info;
	private static final int CALL_DRAWABLE = R.drawable.qcb_ic_call;
	private static final int SMS_DRAWABLE = R.drawable.qcb_ic_sms;
	private static final int EMAIL_DRAWABLE = R.drawable.qcb_ic_email;
	
	public static final List<QuickItem> makeQuickActionList(Context context, Contact contact) {
		List<QuickItem> resultList = new ArrayList<QuickItem>();
		resultList.add(new QuickItem(INFO_DRAWABLE, 0, new ContactInfoClickListener(context, contact)));
		if (contact.getPhones().size() > 0) {
			Phone phone = contact.getPhones().get(0);
			resultList.add(new QuickItem(CALL_DRAWABLE, 0, new PhoneClickListener(context, phone)));
			resultList.add(new QuickItem(SMS_DRAWABLE, 0, new SmsClickListener(context, phone)));
		}
		if (contact.getEmails().size() > 0) {
			Email email = contact.getEmails().get(0);
			resultList.add(new QuickItem(EMAIL_DRAWABLE, 0, new EmailClickListener(context, email)));
		}
		return resultList;
	}
	
	private static class PhoneClickListener implements OnClickListener {

		private final Context mContext;
		private final Phone mPhone;

		public PhoneClickListener(Context context, Phone phone) {
			mContext = context;
			mPhone = phone;
		}

		@Override
		public void onClick(View view) {
			String action = Intent.ACTION_DIAL;
			Uri uri = Uri.parse("tel:" + mPhone.number);
			Intent call = new Intent(action, uri);
			mContext.startActivity(call);
		}
	}

	private static class EmailClickListener implements OnClickListener {

		private final Context mContext;
		private final Email mEmail;

		public EmailClickListener(Context context, Email email) {
			mContext = context;
			mEmail = email;
		}

		@Override
		public void onClick(View view) {
			Intent call = SendMessageUtils.emailIntent(mEmail);
			mContext.startActivity(call);
		}
	}
	
	private static class SmsClickListener implements OnClickListener {

		private final Context mContext;
		private final Phone mPhone;

		public SmsClickListener(Context context, Phone phone) {
			mContext = context;
			mPhone = phone;
		}

		@Override
		public void onClick(View view) {
			Intent call = SendMessageUtils.smsIntent(mPhone);
			mContext.startActivity(call);
		}
		
	}
	
	private static class ContactInfoClickListener implements OnClickListener {
		
		private final Context mContext;
		private final Contact mContact;
		
		public ContactInfoClickListener(Context context, Contact contact) {
			mContext = context;
			mContact = contact;
		}
		
		@Override
		public void onClick(View view) {
			Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, mContact.id);
			Intent activityCall = new Intent(Intent.ACTION_VIEW, contactUri);
			activityCall.setPackage(mContext.getPackageName());
			mContext.startActivity(activityCall);			
		}
		
	}
}
